// CG6-Generator.cpp : Defines the entry point for the console application.
//
//	BOOL GenSetPWM ( DWORD dwPWM );
//	DWORD GenReadRPM ( void );
//	unsigned char GenReadCount ( void );
//	
//	

#include "stdafx.h"
 
#include <windows.h>

#include <commctrl.h>
#include <WinDef.h>
#include <excpt.h>
#include <tchar.h>



HANDLE hGenerator = INVALID_HANDLE_VALUE;

BOOL GenInit( void );
BOOL GenSetPWM ( DWORD dwPWM );
DWORD GenReadRPM ( void );
unsigned char GenReadCount ( void );



//----------------------------------------------------------------------------//
//	Init Generator																														//
//	Return FALSE if error																											//
//----------------------------------------------------------------------------//
BOOL GenInit() {

#ifdef UNDER_CE
	RETAILMSG(1, (L"#######   :FLAG 111\r\n"));
#endif

	DCB dcb;
	COMMTIMEOUTS cto;

	if (hGenerator != INVALID_HANDLE_VALUE)
		CloseHandle ( hGenerator );  

	hGenerator = CreateFile ( TEXT("COM3:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (hGenerator != INVALID_HANDLE_VALUE) {
		dcb.DCBlength = sizeof (dcb);
		GetCommState (hGenerator, &dcb);
		dcb.BaudRate = CBR_9600;
		dcb.fParity = FALSE;
		dcb.fNull = FALSE;
		dcb.StopBits = ONESTOPBIT;
		dcb.Parity = NOPARITY;
		dcb.ByteSize = 8;
		SetCommState (hGenerator, &dcb);

		//cto.ReadIntervalTimeout = 0;
		
		cto.ReadIntervalTimeout = MAXDWORD;
		cto.ReadTotalTimeoutMultiplier = 0;
		cto.ReadTotalTimeoutConstant = 0;
		cto.WriteTotalTimeoutMultiplier = 0;
		cto.WriteTotalTimeoutConstant = 0;
		SetCommTimeouts (hGenerator, &cto);
	} else 
	{
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Open COM3 Failed \r\n")));
#endif
		return FALSE;
	}
	return TRUE;
}

BOOL  GenDeInit()
{
	if( INVALID_HANDLE_VALUE!=hGenerator ) 
		CloseHandle( hGenerator );
	hGenerator = INVALID_HANDLE_VALUE;
	return TRUE;
}


//----------------------------------------------------------------------------//
//	Set PWM: 0 - 100																													//
//	Return FALSE if error																											//
//----------------------------------------------------------------------------//
BOOL GenSetPWM ( DWORD dwPWM ) {
	static char szCommand[] = { 0xfe, 0x02, 0x00, 0x00, 0xfd };
	DWORD dummy;

	if (hGenerator == INVALID_HANDLE_VALUE)
		if ( GenInit() == FALSE )
			return FALSE;  

	szCommand[2] = (char)dwPWM;
	szCommand[3] = (char)dwPWM + 0x02;
	if( ! WriteFile( hGenerator, szCommand, 5, &dummy, 0) ) {
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Set PWM Failed \r\n")));
#endif
		return FALSE;
	};
	return TRUE;
	/******* ????? Wait for return Value ************/
}

//----------------------------------------------------------------------------//
//	Read RPM: 0 - 255																													//
//	Return 0 if error																													//
//----------------------------------------------------------------------------//
DWORD GenReadRPM ( void ) {
	static char szCommand[] = { 0xfe, 0x04, 0x00, 0x04, 0xfd };
	char szReceive[5] = {0};
	DWORD dummy;

	if (hGenerator == INVALID_HANDLE_VALUE) 
		if ( GenInit() == FALSE )
			return 0;  
	if( ! WriteFile( hGenerator, szCommand, 5, &dummy, 0) ) {
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Read RPM write command Failed \r\n")));
#endif
		return 0;
	};
	//Sleep(10);
	Sleep(200);

	if( ! ReadFile( hGenerator, szReceive, 5, &dummy, 0) ) {
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Read RPM Failed \r\n")));
#endif
		return 0;
	};
	return (DWORD)szReceive[2];
}

//----------------------------------------------------------------------------//
//	Read Count: 0xRRRRLLLL  																									//
//	RRRR:Right hand count(0-15),	LLLL:Left	hand count(0-15)									//
//	Return 0 if error																													//
//----------------------------------------------------------------------------//

char g_szReceive[5] = {0};

unsigned char GenReadCount ( void ) {
	static char szCommand[] = { 0xfe, 0x0e, 0x00, 0x0e, 0xfd };
	DWORD dummy;

	if (hGenerator == INVALID_HANDLE_VALUE) 
		if ( GenInit() == FALSE )
			return 0;  
	if( ! WriteFile( hGenerator, szCommand, 5, &dummy, 0) ) {
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Read Count write command Failed \r\n")));
#endif
		return 0;
	};
	//Sleep(10);
	Sleep(200);

	if( ! ReadFile( hGenerator, g_szReceive, 5, &dummy, 0) ) {
#ifdef UNDER_CE
		RETAILMSG(1, (TEXT("[Generator] Read Count Failed \r\n")));
#endif
		return 0;
	};
	return g_szReceive[2];
}
